package com.dai.chun.dao.action;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dai.chun.dao.dao.AdminDao;
import com.dai.chun.dao.dao.EmployeeDao;
import com.dai.chun.dao.impl.AdminDaoImpl;
import com.dai.chun.dao.impl.EmployeeDaoImpl;
import com.dai.chun.dao.model.Admin;
import com.dai.chun.dao.model.Employee;
import com.dai.chun.util.EncrypterUti;
import com.opensymphony.xwork2.ActionContext;



/**
 * 
 * @author Administrator
 *
 */
public class LoginAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Admin admin;
	private String  verifycode;
	public List<String> loginTips=new ArrayList<String>();
	private Employee employee;
	
	
	/**
	 * 登陆逻辑
	 * @throws IOException
	 */
	public String logIn() throws IOException{
		if (!valiImgCode()){
			loginTips.add("验证码错误");
			return "logError";
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		
		if(null!=admin){//在前台没有传来admin有关的参数时 ,struts不会对admin实例化
			admin=adminAuthenticate(admin);
			if(null==admin){
				loginTips.add("用户名或密码错误");
				logger.warn(admin.getAdmin_name()+"登陆失败");
				return "logError";
			}
			
			session.setAttribute("admin", admin);
			return "adminLogIn";
		}else {
			employee=employeeAuthenticate(employee);
			if(null==employee){
				logger.warn(employee.getEmp_name()+"登陆失败");
				return "logError";
			}
			session.setAttribute("employee", employee);
			return "employeeLogIn";
		}
		
	}
	
	/**
	 * 验证管理员登陆密码
	 * @param admin
	 * @return
	 * @throws IOException
	 */
	public Admin adminAuthenticate(Admin admin) throws IOException{
		AdminDao adminDao=new AdminDaoImpl();
		Admin adminNice=adminDao.logIn(admin);
		//判断密码是否相等
		if(EncrypterUti.encrypt(admin.getAdmin_password()).equals(adminNice.getAdmin_password())){
			admin=adminNice;
			return adminNice;
		}
		return null;
	}
	/**
	 * 验证员工登陆密码
	 * @param employee
	 * @return
	 * @throws IOException
	 */
	public Employee employeeAuthenticate(Employee employee) throws IOException{
		EmployeeDao employeeDao=new EmployeeDaoImpl();
		Employee employeeNice=employeeDao.logIn(employee);
		//判断密码是否相等
		if(EncrypterUti.encrypt(employee.getEmp_password()).equals(employeeNice.getEmp_password())){
			employee=employeeNice;
			return employeeNice;
		}
		return null;
	}
	//校验验证码
	public boolean valiImgCode() {
		String str2 = (String) (ActionContext.getContext().getSession()
				.get("random"));// 取得session保存中的字符串
		// 下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了
		if (str2.equals(verifycode)) {
			return true;
		} else {
			return false;
		}

	}
	//退出登陆
	public String logOut(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession currentSession=request.getSession();
		if(currentSession!=null){
			currentSession.invalidate();//销毁session
		}
		return "logOut";
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}
	public List<String> getLoginTips() {
		return loginTips;
	}
	public void setLoginTips(List<String> loginTips) {
		this.loginTips = loginTips;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}
